﻿Namespace Producto

    Public Class frmProductoMant

        Public Property ProductoID As Integer = -1
        Public Property CatalogoId As Integer = -1


        Public ReadOnly Property Catalogado As Integer
            Get
                Dim _bol As Integer = 0
                If CatalogoId > 0 Then
                    _bol = 1
                End If
                Return _bol
            End Get

        End Property


        Public Sub LoadParameter()
            'cargarCombo FormaSimple
            CargarFormaSimple()
            CargarFormaCompuesta(Me.cbbFormaSimple.SelectedValue)
            CargarMarca()
            CargarTipo()
            'Listar()
        End Sub
        Public Sub CargarTipo()
            Dim obj As New Farmacia.Business.Producto.Tipo
            Dim ocol As Farmacia.Entity.Producto.TipoCol
            ocol = obj.Combo(True, "Seleccione")
            Me.cbbTipo.DisplayMember = "Nombre"
            Me.cbbTipo.ValueMember = "TipoId"
            Me.cbbTipo.DataSource = ocol
        End Sub
        Public Sub CargarMarca()
            Dim obj As New Farmacia.Business.Producto.Marca
            Dim ocol As Farmacia.Entity.Producto.MarcaCol
            ocol = obj.Combo(True, "Seleccione")
            Me.cbbMarca.DisplayMember = "Nombre"
            Me.cbbMarca.ValueMember = "MArcaId"
            Me.cbbMarca.DataSource = ocol
        End Sub

        Public Sub CargarFormaSimple()
            Dim obj As New Farmacia.Business.Producto.FormaSimple
            Dim ocol As Farmacia.Entity.Producto.FormaSimpleCol
            ocol = obj.Combo(True, "Seleccione")
            Me.cbbFormaSimple.DisplayMember = "Nombre"
            Me.cbbFormaSimple.ValueMember = "FormaSimpleId"
            Me.cbbFormaSimple.DataSource = ocol
        End Sub

        Public Sub CargarFormaCompuesta(ByVal vFormaSimpleId As Integer)
            Dim obj As New Farmacia.Business.Producto.FormaCompuesta
            Dim oCol As Farmacia.Entity.Producto.FormaCompuestaCol
            oCol = obj.Combo(vFormaSimpleId, True, "Seleccione")
            Me.cbb_FormaCompuesta.DisplayMember = "Nombre"
            Me.cbb_FormaCompuesta.ValueMember = "FormaCompuestaID"
            Me.cbb_FormaCompuesta.DataSource = oCol
        End Sub




        Public Sub Listar(ByVal vProductoID)

            Dim obj As New Farmacia.Business.Producto.Producto
            Dim ent As Farmacia.Entity.Producto.Producto

            ent = obj.Listar(vProductoID)

            If ent IsNot Nothing Then
                With ent
                    ProductoID = ent.ProductoId
                    Me.CatalogoId = ent.CatalogoId
                    Me.txt_codigo.Enabled = False
                    Me.txt_codigo.Text = ent.Codigo
                    Me.txt_Concentracion.ReadOnly = True
                    Me.txt_Concentracion.Text = ent.Concentracion
                    Me.cbbFormaSimple.SelectedValue = ent.FormaCompuestaId
                    Me.cbb_FormaCompuesta.SelectedValue = ent.FormaCompuestaId
                    Me.nup_fraccion.Value = ent.Fracciones
                    Me.cbbMarca.SelectedValue = ent.MarcaId
                    Me.txt_nombre.Text = ent.Nombre
                    Me.txt_nombre.Text = ent.Presentacion
                    Me.txtDescripcion.Text = ent.ProductoDescripcion
                    Me.chk_generico.Checked = ent.ProductoGenerico
                    Me.txt_observacion.Text = ent.ProductoObservacion
                    Me.cbbTipo.SelectedValue = ent.TipoId

                    Me.txt_stock.Text = ent.ProductoStock
                    Me.txt_precio_unitario.Text = ent.ProductoPrecioUnitario
                    Me.txt_PrecioPromedio.Text = ent.ProductoPrecioPromedio

                End With
            Else
                Vichama.Configurations.Aplication.MessageBox.MensajeInformation("No se puede encontrar el registro")

            End If




        End Sub


        Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
            Me.Close()

        End Sub

        Private Sub btnCatalogo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        End Sub


        Private Sub frmProductoMant_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            LoadParameter()



            If ProductoID > 0 Then
                'Producto >0
                Listar(ProductoID)
            ElseIf CatalogoId > 0 Then
                'producto nuevo catalogoid >0
                '1 es farmaceutico
                Me.cbbTipo.SelectedValue = 1
                Me.cbbTipo.Enabled = False
                Dim obj As New Farmacia.Business.Producto.Catalogo
                Dim ent As Farmacia.Entity.Producto.Catalogo
                ent = obj.Listar(CatalogoId)
                Me.txt_codigo.ReadOnly = True
                Me.txt_codigo.Text = ent.Codigo
                Me.txt_Concentracion.ReadOnly = True
                Me.txt_Concentracion.Text = ent.Concentracion
                Me.txt_nombre.ReadOnly = True
                Me.txt_nombre.Text = ent.Nombre
                Me.cbbFormaSimple.SelectedValue = ent.FormaSimpleId
                Me.cbbFormaSimple.Enabled = False
                Me.cbb_FormaCompuesta.SelectedValue = ent.FormaID
                Me.cbb_FormaCompuesta.Enabled = False
                Me.nup_fraccion.Value = ent.Fracciones
                Me.nup_fraccion.ReadOnly = True
                Me.nup_fraccion.Enabled = False
                Me.chk_generico.Checked = ent.Generico
                Me.chk_generico.Enabled = False
                Me.cbbMarca.SelectedValue = ent.MarcaId
                Me.cbbMarca.Enabled = False
                Me.txt_presentacion.ReadOnly = True
                Me.txt_presentacion.Text = ent.Presentacion
                Me.txt_Registro.ReadOnly = True
                Me.txt_Registro.Text = ent.RegistroSanitario



            Else
                'producto nuevo catalogid -1
                CatalogoId = -1
            End If
        End Sub

        Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click
            If Validar Then

            
            Dim obj As New Farmacia.Business.Producto.Producto
            ProductoID = obj.Grabar(ProductoID, _
                       Catalogado, _
                       CatalogoId, _
                        Me.cbbTipo.SelectedValue, _
                        Me.cbbMarca.SelectedValue, _
                        Me.txt_nombre.Text, _
                         Me.txt_codigo.Text, _
                         Me.cbbFormaSimple.SelectedValue, _
                          Me.cbb_FormaCompuesta.SelectedValue, _
                          Me.txt_Concentracion.Text, _
                          Me.txt_presentacion.Text, _
                          Me.nup_fraccion.Value.ToString, _
                          Me.txt_stock.Text, _
                           Me.txt_precio_unitario.Text, _
                           Me.txt_PrecioPromedio.Text, _
                       Me.chk_generico.Checked, _
                           Me.txtDescripcion.Text, _
                           Me.txt_observacion.Text, _
                           Vichama.Configurations.Usuario.Codigo)

            If ProductoID > 0 Then
                Vichama.Configurations.Aplication.MessageBox.MensajeGrabar()
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            Else
                Vichama.Configurations.Aplication.MessageBox.MensajeError("No se ha podido registrar los datos")
            End If



            End If

        End Sub

        Public Function Validar() As Boolean
            Dim _bol As Boolean = False

            If cbbMarca.SelectedValue = -1 Then
                _bol = False
            End If
            If Me.txt_nombre.Text.Length = 0 Then
                _bol = False
            End If

            Return _bol
            

        End Function

        Private Sub cbbTipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbTipo.SelectedIndexChanged
            If Me.cbbTipo.SelectedValue <> 1 Then

                Me.txt_Registro.Text = ""
                Me.txt_Registro.ReadOnly = True

                Me.chk_generico.Enabled = False

                Me.txt_Concentracion.Text = ""
                Me.txt_Concentracion.ReadOnly = False

                Me.nup_fraccion.ReadOnly = False
                Me.nup_fraccion.Value = 0
                Me.nup_fraccion.Enabled = False

            End If
        End Sub
    End Class
End Namespace